/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * RemoteFileLocationEditor.java
 * 
 * Created on Jan 18, 2009 at 1:38:19 PM
 */

package org.robcash.apps.messagepopup.view.components;

import java.net.MalformedURLException;
import java.net.URL;
import org.robcash.commons.swing.wizard.WizardComponent;

/**
 * Editor for remote file location
 * @author Rob
 */
public class RemoteFileLocationEditor extends WizardComponent {

    private boolean urlValid = false;

    /** Creates new form RemoteFileLocationEditor */
    public RemoteFileLocationEditor() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        instruction = new javax.swing.JLabel();
        urlTextField = new javax.swing.JTextField();
        errorLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.robcash.apps.messagepopup.Main.class).getContext().getResourceMap(RemoteFileLocationEditor.class);
        instruction.setText(resourceMap.getString("instruction.text")); // NOI18N
        instruction.setName("instruction"); // NOI18N

        urlTextField.setText("http://"); // NOI18N
        urlTextField.setName("urlTextField"); // NOI18N
        urlTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                urlTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                urlTextFieldFocusLost(evt);
            }
        });

        errorLabel.setForeground(resourceMap.getColor("errorLabel.foreground")); // NOI18N
        errorLabel.setLabelFor(urlTextField);
        errorLabel.setText(resourceMap.getString("errorLabel.text")); // NOI18N
        errorLabel.setName("errorLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(errorLabel))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(urlTextField, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(instruction, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(115, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(instruction)
                .addGap(18, 18, 18)
                .addComponent(urlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(errorLabel)
                .addContainerGap(212, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public URL getRemoteFileLocation() throws MalformedURLException {
        return new URL(urlTextField.getText());
    }

    public void setRemoteFileLocation(URL url) {
        urlTextField.setText(url.toString());
    }

    private void urlTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_urlTextFieldFocusLost
        // Validate text field to see if it is a URL
        boolean valid = isUrlValid();
        errorLabel.setVisible(!valid);
        if ( valid ^ urlValid ) {
            // Validity has changed
            fireChangeListenerStateChanged();
        }
    }//GEN-LAST:event_urlTextFieldFocusLost

    private void urlTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_urlTextFieldFocusGained
        this.urlValid = isUrlValid();
    }//GEN-LAST:event_urlTextFieldFocusGained

    private boolean isUrlValid() {
        boolean valid = false;
        try {
            URL url = new URL(urlTextField.getText());
            valid = true;
        }
        catch(MalformedURLException e) {
            // Not valid
        }
        return valid;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel instruction;
    private javax.swing.JTextField urlTextField;
    // End of variables declaration//GEN-END:variables

}
